package com.robam.gulimall.product.service.impl;

import com.robam.common.constant.ProductConstant;
import com.robam.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.robam.gulimall.product.entity.AttrGroupEntity;
import com.robam.gulimall.product.service.AttrAttrgroupRelationService;
import com.robam.gulimall.product.service.AttrGroupService;
import com.robam.gulimall.product.service.CategoryService;
import com.robam.gulimall.product.vo.AttrRespVo;
import com.robam.gulimall.product.vo.AttrVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.robam.common.utils.PageUtils;
import com.robam.common.utils.Query;

import com.robam.gulimall.product.dao.AttrDao;
import com.robam.gulimall.product.entity.AttrEntity;
import com.robam.gulimall.product.service.AttrService;
import org.springframework.transaction.annotation.Transactional;


@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {

    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                new QueryWrapper<AttrEntity>()
        );
        return new PageUtils(page);
    }

    @Override
    public PageUtils getAttrByCatId(Map<String, Object> params, Long catId, int attrType) {
        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("attr_type", attrType);
        if(catId != 0) {
            wrapper.eq("catelog_id", catId);
        }
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                wrapper
        );
        // 获取分组和分类的名称，将对象转为VO
        List<AttrEntity> records = page.getRecords();
        List<AttrRespVo> attrRespVos = records.stream().map(attr -> {
            return this.getAttrRespVo(attr);
        }).collect(Collectors.toList());

        PageUtils pageUtils = new PageUtils(page);
        pageUtils.setList(attrRespVos);
        return pageUtils;
    }

    @Override
    @Transactional
    public void saveAttr(AttrVo attr) {
        // 保存属性信息
        AttrEntity attrEntity = new AttrEntity();
        BeanUtils.copyProperties(attr, attrEntity);
        this.baseMapper.insert(attrEntity);
        // 如果填了属性分组,一并保存属性和分组的关联信息
        if(attr.getAttrType() == ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode() &&
            attr.getAttrGroupId() != null) {
            AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
            relationEntity.setAttrId(attrEntity.getAttrId());
            relationEntity.setAttrGroupId(attr.getAttrGroupId());
            attrAttrgroupRelationService.save(relationEntity);
        }
    }

    @Override
    public AttrRespVo getAttrInfo(Long attrId) {
        AttrEntity attrEntity = this.baseMapper.selectById(attrId);
        return this.getAttrRespVo(attrEntity);
    }

    private AttrRespVo getAttrRespVo(AttrEntity attrEntity) {
        AttrRespVo attrRespVo = new AttrRespVo();
        if(attrEntity != null) {
            BeanUtils.copyProperties(attrEntity, attrRespVo);
            // 获取分组和分类的名称
            if (attrRespVo.getAttrType() == ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode()) {
                AttrAttrgroupRelationEntity relationEntity = attrAttrgroupRelationService.getOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrRespVo.getAttrId()));
                if (relationEntity != null) {
                    AttrGroupEntity groupEntity = attrGroupService.getById(relationEntity.getAttrGroupId());
                    attrRespVo.setGroupName(groupEntity.getAttrGroupName());
                }
            }
            // 三级分类名称和路径
            attrRespVo.setCatelogName(categoryService.getById(attrRespVo.getCatelogId()).getName());
            attrRespVo.setCategoryPath(categoryService.getCategoryPath(attrRespVo.getCatelogId()));
        }
        return attrRespVo;
    }

}